<?php

namespace app\admin\controller;

use app\models\CampusModel;
use app\models\CompanyModel;
use app\models\MerchantModel;
use app\models\MpModel;

class Merchant extends Common
{
	/**
	 * 商户管理模型.
	 *
	 * @var MerchantModel
	 */
	protected $merchantModel;

	/**
	 * 集团管理模型.
	 *
	 * @var CompanyModel
	 */
	protected $companyModel;

	/**
	 * 校区管理模型.
	 *
	 * @var CampusModel
	 */
	protected $campusModel;

	/**
	 * 商户管理列表.
	 * @return mixed
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 */
	public function lists()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$merchant = $this->merchantModel->getMerchantLists($param);
			$this->success('success', '', $merchant, 0);
		}
		$where = [];
//		if(get_current_type() != 1){   //非系统
//			$where['id'] = get_current_company_id();
//		}
		$companys = $this->companyModel->all($where);
		$type = get_current_type();
		return $this->fetch('',['type' => $type, 'companys'=>$companys]);
	}

	/**
	 * 新增商户.
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function add()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			$data['company_id'] = $this->campusModel->get($data['campus_id'])['company_id'];
			if (false !== $this->merchantModel->save($data)) {
				$this->success('添加成功', url('Merchant/lists'), '', 1);
			}
			$this->error('添加失败', null, '', 1);
		}

		$where = [];
//		if (get_current_type() == 2){
//			$where['id'] = ['in',get_current_campus_id()];
//		}
		$where['status'] = 1;
		$campus = $this->campusModel->all($where);
		$mp = (new MpModel())->all($where);
		$this->dictionary();
		return $this->fetch('',[
			'campus' => $campus,
			'mp' => $mp,
		]);
	}

	/**
	 * 编辑商户.
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			$data['company_id'] = $this->campusModel->get($data['campus_id'])['company_id'];
			if (false !== $this->merchantModel->save($data,['id'=>$data['id']])) {
				$this->success('编辑成功', url('Merchant/lists'), '', 1);
			}
			$this->error('添加失败', null, '', 1);
		}

		$where = [];
//		if (get_current_type() == 2){
//			$where['id'] = ['in',get_current_campus_id()];
//		}
		$where['status'] = 1;
		$campus = $this->campusModel->all($where);
		/*获取商户信息*/
		$mch = $this->merchantModel->get(input('id'));
		$mp = (new MpModel())->all($where);
		$this->dictionary();
		return $this->fetch('',[
			'campus' => $campus,
			'mp' => $mp,
			'mch'=>$mch
		]);
	}

	/**
	 * 删除.
	 *
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			if (!$this->classRoomTimeModel->where(['id'=>input('id')])->delete()) {
				$this->error('删除失败', null, '', 1);
			}
			$this->success('删除成功', url('Campus/lists'), '', 1);
		}
	}

	/**
	 * 状态.
	 *
	 * @return mixed
	 */
	public function status()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			if($data['mch_status'] == 1){
				$msg = '启用';
			}else{
				$msg = '禁用';
			}
			if (!$this->merchantModel->save($data, ['id' => $data['id']])) {
				$this->error($msg.'失败', null, '', 1);
			}
			$this->success($msg.'成功', '', '', 1);
		}
	}

	/**
	 * 实例化.
	 *
	 * @return bool|void
	 */
	protected function _initialize()
	{
		parent::_initialize();
		$this->merchantModel = new MerchantModel();
		$this->companyModel = new CompanyModel();
		$this->campusModel = new CampusModel();
	}
}
